package com.sky.mapper.courier;

import com.sky.pojo.CourierTradeOrder;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

public interface CourierTradeOrderMapper {
    List<Map<String, Long>> findTodayOrdersNumByIds(@Param("courierIds") List<Long> courierIds,
                                                    @Param("today") String today);

    List<Long> findTodayOrdersLessThen12ByIds(@Param("courierIds")List<Long> courierIds,
                                              @Param("today") String today);

    @Insert("INSERT INTO tb_courier_trade_order VALUES(NULL,#{totalAmount},#{courierId},#{orderNumber},#{waybillStatus},#{tradeCreateTime},#{tradeCompleteTime},#{tradeCancelTime},#{overTime},#{shopAddress},#{remark},#{customerAddress},#{customerExpectedDeliveryTime},#{customerName},#{shopName},#{toBeMealTime},#{telephone},#{income})")
    void save(CourierTradeOrder courierTradeOrder);

    @Select("select * from tb_courier_trade_order where waybill_status=#{status} and courier_id=#{courierId}")
    List<CourierTradeOrder> selectOrdersByStatusAndCourierId(@Param("status") Integer status,
                                                             @Param("courierId") Long courierId);

    @Select("select * from tb_courier_trade_order where order_number=#{tradeNo}")
    CourierTradeOrder selectByOrderNumber(String tradeNo);

    @Update("update tb_courier_trade_order set waybill_status=#{status} where trade_no=#{tradeNo}")
    void updateStatusByTradeNo(@Param("tradeNo") Long tradeNo,
                               @Param("status") Integer status);

    @Select("select * from tb_courier_trade_order where trade_no=#{tradeNo}")
    CourierTradeOrder selectByTradeNo(Long tradeNo);

    @Update("update tb_courier_trade_order set waybill_status=#{status},trade_complete_time=#{completeTime} where trade_no=#{tradeNo}")
    void updateStatusAndCompleteTimeByTradeNo(@Param("status") Integer status,
                                              @Param("completeTime") LocalDateTime now,
                                              @Param("tradeNo") Long tradeNo);

    @Update("update tb_courier_trade_order set courier_id=#{courierId} where trade_no=#{tradeNo}")
    void updateCourierIdByTradeNo(@Param("courierId") Long courierId,
                                  @Param("tradeNo") Long tradeNo);


    List<CourierTradeOrder> selectCourierOrders(@Param("date") String date,
                                                @Param("status") Integer status,
                                                @Param("courierId") Long courierId);

    Double selectOrdersCompleteAvgTime(@Param("courierId") Long courierId,
                                       @Param("startTime") String startTime,
                                       @Param("endTime") String endTime);


    Long selectOrdersCountByCourierIdAndDate(@Param("courierId") Long courierId,
                                             @Param("startTime") String startTime,
                                             @Param("endTime") String endTime,
                                             @Param("status")Integer status);

    Long selectOnTimeOrdersByCourierIdAndDate(@Param("courierId") Long courierId,
                                              @Param("startTime") String startTime,
                                              @Param("endTime") String endTime);

    Map<String, Long> selectEveryDayOrdersCountByDateAndStatus(@Param("start") String start,
                                                               @Param("end") String end,
                                                               @Param("dateList") List<String> dateList,
                                                               @Param("status") Integer status);
}
